//
//  UIAlertController+LSF.swift
//  hoonpay
//
//  Created by 李胜锋 on 2017/8/7.
//  Copyright © 2017年 lishengfeng. All rights reserved.
//

import Foundation
import UIKit

func lsf_alert(_ title:String? = nil,
               message:String? = nil,
               style:UIAlertControllerStyle = .alert,
               viewController:UIViewController? = nil,
               actionTitles:[String]? = nil,
               cancelTitle:String? = nil,
               destructiveTitle:String? = nil,
               sourceView:UIView? = nil,
               handle:( (_ title:String) -> Void )? = nil ) {
    let alertController = UIAlertController(title: title, message: message, preferredStyle: style)
    
    //Action
    if let actions = actionTitles {
        for action in actions {
            let alertAction = UIAlertAction(title: action, style: .default, handler: { (alertAction) in
                 handle?(action)
            })
            alertController.addAction(alertAction)
        }
    }
    
    var destructive:String?
    if destructiveTitle != nil {
        destructive = destructiveTitle
    }
    else if cancelTitle == nil && actionTitles == nil {
        destructive = "确认"
    }
    // 确认按钮
    if let str = destructive {
        let destructiveAction = UIAlertAction.init(title: str, style: .destructive, handler: { (alertAction) in
            handle?(str)
        })
        alertController.addAction(destructiveAction)
    }
    
    //取消按钮
    if let cancel = cancelTitle {
        let cancelAction = UIAlertAction.init(title: cancel, style: .cancel, handler: { (alertAction) in
            handle?(cancel)
        })
        alertController.addAction(cancelAction)
    }
    
    
   
    
    var vc = viewController
    if viewController == nil {
        vc = UIViewController.lsf_currentVC()
    }
    
    if  style == .actionSheet {
        let popOverController = alertController.popoverPresentationController
        
        if let view = sourceView {
            popOverController?.sourceView = view
            popOverController?.sourceRect = view.bounds
        }
        else {
            
            popOverController?.sourceView = vc?.view
            var rect = vc!.view.bounds
            rect.size.height -= 300
            popOverController?.sourceRect = rect
        }
    }
    
    vc?.modalPresentationStyle = .popover
    vc?.present(alertController, animated: true, completion: nil)
}

///显示带输入框的提示框
func lsf_showTextFieldAlert(title: String?,
                            placeholder: String?,
                            presentController: UIViewController,
                            complete: ( (String?) -> Void)?) {
    let alertVC = UIAlertController.init(title: title, message: nil, preferredStyle: .alert)
    alertVC.addTextField { (tf) in
        tf.placeholder = placeholder
    }
    
    let cancelAction = UIAlertAction.init(title: "取消", style: .cancel, handler: nil)
    let okAction = UIAlertAction.init(title: "确认", style: .default) { (action) in
        guard  let textField = alertVC.textFields?.first else {  return }
        let str = textField.text
        complete?(str)
    }
    alertVC.addAction(cancelAction)
    alertVC.addAction(okAction)
    presentController.present(alertVC, animated: true, completion: nil)
}
